<template>
  <el-dialog class="VideoUpload-dlg" title="视频信息" width="45%" :visible.sync="innerVisible">
    <el-form ref="VideoUploadForm" :model="VideoUploadRecord" :rules="VideoUploadRules"
    label-position="right" label-width="105px" size="mini" status-icon>
      <el-row>
        <el-col :span="12">
          <el-form-item label="视频名称" prop="SPMSMC">
            <el-input type="textarea" :rows="1" v-model="VideoUploadRecord.SPMSMC"></el-input>
          </el-form-item>
          <el-form-item label="关键字" prop="SPGJZ">
            <el-input type="textarea" :rows="1" v-model="VideoUploadRecord.SPGJZ"></el-input>
          </el-form-item>
          <el-form-item label="视频类型" prop="SPMSLX">
            <el-select style="width:100%;" v-model="VideoUploadRecord.SPMSLX" placeholder="请选择">
              <template v-for="item in LXData">
                <el-option :key="item.LX" :label="item.LX" :value="item.LX">
                  <span style="float: left">{{item.LX}}</span>
                </el-option>
              </template>
            </el-select>
          </el-form-item>
          <el-form-item label="视频简介" prop="SPMSJJ">
            <el-input type="textarea" :rows="4" v-model="VideoUploadRecord.SPMSJJ"></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="封面图片"></el-form-item>
          <el-upload
            v-if="true"
            class="avatar-uploader"
            action="/file"
            :show-file-list="false"
            :on-success="(res,file)=>{$emit('upload-success1',res,file);}"
            :before-upload="beforeAvatarUpload"
            :data="{'type':'VideoUpload'}">
            <img v-if="onUpdateSrc()" style="width:auto" :src="VideoUploadUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-col>
      </el-row>

      <el-row>
        <el-form-item label="上传视频"></el-form-item>
          <el-upload
            v-if="true"
            class="avatar-uploader"
            action="/file"
            :show-file-list="false"
            :on-success="(res,file)=>{$emit('upload-success2',res,file);}"
            :before-upload="VideobeforeAvatarUpload"
            :data="{'type':'VideoUpload'}">
            <el-button v-if="onUpdateSrc2()" size="small" type="primary">重新上传视频</el-button>
            <el-button v-else size="small" type="primary">点击上传视频</el-button>
        </el-upload>
      </el-row>

      <el-checkbox v-model="VideoUploadRecord.SPMSZT" label="启用" true-label="1" false-label="0">
      </el-checkbox>
      
      <el-row>
        <el-button @click="onConfirm" type="primary" size="mini">确定</el-button>
        <el-button @click="innerVisible=false" size="mini">取消</el-button>
      </el-row>
    </el-form>
  </el-dialog>   
</template>
<script>

export default {
  name:"VideoUploadDlg",
  props:{
    VideoUploadRecord:{
      type:Object,
      default:()=>{
        return {};
      }
    },
    VideoUploadRules:{
      type:Object,
      default:()=>{
        return {};
      }
    },
  VideoUploadDlgVisible:{
      type:Boolean,
      default:false
    },
    hasResetBtn:{
      type:Boolean,
      default:false
    }
  },
  watch:{
   VideoUploadDlgVisible: function (val, oldVal) {
      this.innerVisible = val;
    },
    innerVisible:function(val, oldVal){
      if(!val){
        this.$emit('close');
      }
    }
  },
  methods:{
    getFileURL:function(file) { //检查URL
      let url = null ;
      if (window.createObjectURL!=undefined) { // basic
        url = window.createObjectURL(file) ;
      }else if (window.webkitURL!=undefined) { // webkit or chrome
        url = window.webkitURL.createObjectURL(file) ;
      }else if (window.URL!=undefined) { // mozilla(firefox)
        url = window.URL.createObjectURL(file) ;
      }
      return url;
    },
    beforeAvatarUpload:function(file) {//封面图片上传前检查
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }

      this.VideoUploadRecord.SPMSFMTPURL =  this.getFileURL(file); //检查URL
      this.VideoUploadUrl = this.VideoUploadRecord.SPMSFMTPURL;
      
      return isJPG && isLt2M && (this.VideoUploadUrl != "");
    },
    VideobeforeAvatarUpload:function(file) {//视频上传前检查
      const ismp4 = file.type === 'video/mp4';
      const isogg = file.type === 'video/ogg';
      const isflv = file.type === 'video/flv';
      const isavi = file.type === 'video/avi';
      const iswmv = file.type === 'video/wmv';
      const isrmvb = file.type === 'video/rmvb';
      const ismov = file.type === 'video/mov';
      const isqlv = file.type === 'video/qlv';
      
      
      const isLt5GB = file.size / 1024 / 1024 < (1024 * 5);

      if (!ismp4 && !isogg && !isflv && !isavi && !iswmv && !isrmvb && !ismov && !isqlv) {
        this.$message.error('上传视频格式错误!');
      }
      if (!isLt5GB) {
        this.$message.error('上传视频大小不能超过 5GB!');
      }

      this.VideoUploadRecord.MTKURL = this.getFileURL(file); //检查URL
      this.VideoSectionUrl = this.VideoUploadRecord.MTKURL

      return  (ismp4 || isogg || isflv || isavi || iswmv || isrmvb || ismov || isqlv) && isLt5GB && (this.VideoSectionUrl != "");
    },
    onConfirm:function(){
      let me = this;
      // 验证信息
      let ret;
      me.$refs.VideoUploadForm.validate((result, what) => {
        ret = result;
      });
      if (!ret) {
        me.$message.error("表单信息有误");
        return;
      }else{
        me.$emit('confirm');
      }

    },
    onUpdateSrc:function(){
      if(this.VideoUploadRecord.SPMSFMTPURL != "" )
      {
        this.VideoUploadUrl = this.VideoUploadRecord.SPMSFMTPURL;
        return true;
      }
      else{
        return false;
      }
    },
    onUpdateSrc2:function(){
      if(this.VideoUploadRecord.MTKURL != "" )
      {
        this.VideoSectionUrl = this.VideoUploadRecord.MTKURL;
        return true;
      }
      else{
        return false;
      }
    },
  },
  data(){
    return {
      innerVisible:false,
      departmentData:[],
      VideoUploadUrl: '',
      VideoSectionUrl: '',
      fileList: [],
      LXData:[],
    };
  },
  created: function() {
    var me = this;

    // 视频类型
    me.getRequest("/static/json/spmslx.json",{}).then(resp=>{
      if(resp.status == 200) {
        me.LXData = resp.data;
      }
    });

  }
}
</script>
<style lang="scss">
@import "@/assets/scss/basic.scss";
.VideoUpload-dlg{
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: $--color-primary;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 134px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 134px;
    height: 178px;
    display: block;
  }
  .module-basic-VideoSection {
  height: 100%;
}
}
</style>
